
import { LabelUtil } from "./LabelUtil";
import TalkPopMenu from "./TalkPopMenu";

// requireComponent, disallowMultiple, executeInEditMode
const { ccclass, property } = cc._decorator;

@ccclass
export default class TalkShow extends cc.Component {
    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = '';

    @property(TalkShow)
    nextTalk: TalkShow = null;

    /*
    @property([cc.String])
    public textabc:string[] = [];

    @property(
        {
            multiline:true,
            displayName: '表格内容',
        }
    )
    csvContent: string = '';


    @property
    _file: cc.TextAsset = null;
    @property(
    {
        type:cc.TextAsset,
        displayName: 'csv文件',
    })
    get file(): cc.TextAsset {
        return this._file;
    }
    set file(newEventParam: cc.TextAsset) {
        this.notify(this._eventParams, newEventParam);
        this.csvContent = newEventParam.text;
        this._file = newEventParam;
    }

    @property
    _eventParams: string = '';
    @property({
        displayName: '事件参数',
        tooltip: '触发...',
        type: cc.String,
        multiline:true
    })
    get eventParam(): string {
        return this._eventParams;
    }
    set eventParam(newEventParam: string) {
        this.notify(this._eventParams, newEventParam);
        this._eventParams = newEventParam;
    }
    
    notify(oldEventParam: string, newEventParam: string) {
        this.text111 = newEventParam;
        cc.log(oldEventParam, newEventParam);
    }

    */


    public canClick = false;
    private talkStayDuration: number = 1;
    private talkFadeDuration: number = 0.3;

    onEnable() {
        let popMenu = this.label.node.parent.getComponent("TalkPopMenu");
        if (popMenu != null) {
            let showDuration = popMenu.showTalk(this.text);
            this.scheduleOnce(function () {
                this.canClick = true;
            }, showDuration)

        }
        else {
            this.label.node.parent.active = true;
            this.label.node.parent.stopAllActions();
            this.label.node.parent.runAction(cc.fadeIn(this.talkFadeDuration));
            this.label.string = this.text;

            let labelSize = new cc.Size(280, 0);
            let size = LabelUtil.measureSize(this.text, labelSize, this.label.fontSize, this.label.lineHeight);
            let qipao = this.label.node.parent.getChildByName("qipao");
            qipao.setContentSize(size.width + 40, size.height + 40);
            this.label.node.setContentSize(size);
            this.scheduleOnce(function () {
                this.canClick = true;
            }, this.talkStayDuration)
        }
    }

    public showNextTalk(): TalkShow {
        cc.log(`显示[showNextTalk]`);
        if (!this.canClick) {
            return this;
        }
        this.canClick = false;
        this.scheduleOnce(function () {
            this.canClick = true;
        }, this.talkStayDuration);


        if (this.nextTalk == null) {
            this.finishTalk();
            return null;
        }
        this.nextTalk.node.active = true;
        this.node.active = false;
        if (this.label != this.nextTalk.label) {
            this.finishTalk();
        }
        return this.nextTalk;

    }

    public finishTalk(): void {
        cc.log("finishTalk...");
        let popMenu = this.label.node.parent.getComponent("TalkPopMenu");
        if (popMenu != null) {
            popMenu.dealClickTalk(false);
        }
        else {
            this.label.node.parent.runAction(cc.fadeOut(this.talkFadeDuration));
        }
    }

    // update (dt) {}
}
